﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Zhuang.WinForm.AppCode.Common;
using Zhuang.WinForm.AppCode.Enumeration;
using Zhuang.Utility.Common.DataBase;
using Zhuang.Utility.Common.Encryption;

namespace Zhuang.WinForm
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + @"\AppFolder\Skin\Skin.ssk";
        }

        private void btLogin_Click(object sender, EventArgs e)
        {

            //MessageBox.Show(SystemUtility.connectionString);
            //return;


            LoginResult reslut = LoginResult.NO;
            reslut = checkUserPassword(tbUserName.Text.Trim(), tbPassword.Text.Trim());
            if (reslut == LoginResult.UserNotExists)
            {
                MessageBox.Show("用户名不存在！");
                tbUserName.Focus();
                tbUserName.SelectAll();
            }
            else if (reslut == LoginResult.PassWordWorng)
            {
                MessageBox.Show("密码错误！");
                tbPassword.Focus();
                tbPassword.SelectAll();
            }
            else
            {
                this.Visible = false;
                Main main = new Main();
                main.Show();


                //initializeSql();
            }
        }


        private LoginResult checkUserPassword(string username, string password)
        {
            LoginResult result = LoginResult.NO;

            Crypto ch = new  Crypto();
            password = ch.GetEncryptedValue(password);


            if (username != SystemUtility.userName)
            {
                result = LoginResult.UserNotExists;
            }
            else if (password != SystemUtility.userPassword)
            {
                result = LoginResult.PassWordWorng;
            }

            return result;
        }


        private void Login_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            LoginSetting ls = new LoginSetting();
            ls.ShowDialog();
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }

        private void initializeSql()
        {
            string strSql = "";

            strSql = strSql + @"if object_id('App_User', 'U') is null
                    begin
                    create table App_User
                    (userId int identity(0,1) primary key,
                    userName varchar(50) unique,
                    userPassword varchar(50) default '')
                    end;";

            strSql = strSql + @"if object_id('App_Menu', 'U') is null
                    begin
                    create table App_Menu
                    (menuId int identity(0,1) primary key,
                    menuName varchar(50) unique)
                    end;";

            strSql = strSql + @"if object_id('App_UserToMenu', 'U') is null
                    begin
                    create table App_UserToMenu
                    (userId int,
                    menuId int)
                    end";

            SqlHelper.ExecuteSql(strSql);
        }

        private void tbUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                tbPassword.Focus();
            }
            
        }

        private void tbPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin.Focus();
            }
        }
    }
}
